package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("DetectController")
public class DetectController extends CmiEmaController {

	@RequestMapping("/listDetects")
	public ModelAndView listDetects(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = detectPaginationService.updatePagination(request, pagination);
		Set<Detect> detects = detectPaginationService.getPaginationDetects(pagination);

		mav.addObject("detects", detects);
		mav.addObject("entityName", "detect");
		mav.addObject("pagination", pagination);
		mav.setViewName("detect/listDetects.jsp");
		return mav;
	}

	public String listDetects() {
		return "redirect:/listDetects";
	}

	@RequestMapping("/viewDetect")
	public ModelAndView viewDetect(@RequestParam Integer detectId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("detect", detectDAO.findDetectByPrimaryKey(detectId));
		mav.setViewName("detect/viewDetect.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteDetects")
	public ModelAndView confirmDeleteDetects(
			@RequestParam(value = "deleteDetectsIds", required = true) Set<Integer> deleteDetectsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Detect> deleteDetects = detectDAO.findDetectsByIds(deleteDetectsIds);
		Set<Detect> referencedDetects = detectService.loadReferenceDetects(deleteDetects);
		boolean hasReferenced;
		if (referencedDetects.size() == 0) {
			mav.addObject("deleteEntitys", deleteDetects);
			hasReferenced = false;
		} else {
			deleteDetects.removeAll(referencedDetects);
			mav.addObject("referencedEntitys", referencedDetects);
			mav.addObject("deleteEntitys", deleteDetects);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Detect");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listDetects");
		return mav;
	}

	@RequestMapping("/deleteDetect")
	public String deleteDetect(@RequestParam Integer detectId) {
		Detect detect = detectDAO.findDetectByPrimaryKey(detectId);
		detectService.deleteDetect(detect);
		return "forward:/listDetects";
	}

	@RequestMapping("/confirmDeleteDetect")
	public ModelAndView confirmDeleteDetect(@RequestParam Integer detectId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("detect", detectDAO.findDetectByPrimaryKey(detectId));

		mav.setViewName("detect/deleteDetect.jsp");

		return mav;
	}

	@RequestMapping("/deleteDetects")
	public String deleteDetects(
			@RequestParam(value = "deleteDetectsId", required = true) Set<Integer> deleteDetectsId) {
		detectService.deleteDetects(deleteDetectsId);
		return "forward:/listDetects";
	}

	@RequestMapping("/editDetect")
	public ModelAndView editDetect(@RequestParam Integer detectId) {

		ModelAndView mav = new ModelAndView();

		Detect detect = detectDAO.findDetectByPrimaryKey(detectId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("detect", detect);

		// 把所有的引用detect对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectService.loadDetectObjectReferencedSet(detect));

		// 将编辑页面detect添加到mav中
		mav.setViewName("detect/editDetect.jsp");
		return mav;
	}

	@RequestMapping("/newDetect")
	public ModelAndView newDetect() {
		ModelAndView mav = new ModelAndView();
		Detect detect = new Detect();

		mav.addObject("detect", detect);

		// 把所有的引用detect对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectService.loadDetectObjectReferencedSet(detect));

		mav.addObject("newFlag", true);
		mav.setViewName("detect/editDetect.jsp");

		return mav;
	}

	@RequestMapping("/saveDetect")
	public ModelAndView saveDetect(@ModelAttribute Detect detect) {

		ModelAndView mav = new ModelAndView();

		Detectdepartment detectdepartmentTemp = detect.getDetectdepartment();
		Detectdepartment detectdepartment = detectdepartmentTemp == null ? null
				: CmiHibernateProxyUtil
						.deproxy(detectdepartmentDAO.findDetectdepartmentById(detectdepartmentTemp.getId()));
		Foodbatch foodbatchTemp = detect.getFoodbatch();
		Foodbatch foodbatch = foodbatchTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(foodbatchDAO.findFoodbatchById(foodbatchTemp.getId()));
		User detectorTemp = detect.getDetector();
		User detector = detectorTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(userDAO.findUserById(detectorTemp.getId()));
		Subject subjectTemp = detect.getSubject();
		Subject subject = subjectTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(subjectDAO.findSubjectById(subjectTemp.getId()));

		Detect existingDetect = CmiHibernateProxyUtil.deproxy(detectDAO.findDetectByPhysicalPrimaryKey(subject.getId(),
				foodbatch.getId(), detect.getDate(), detector.getId(), detectdepartment.getId()));

		detect.setDetectdepartment(detectdepartment);
		detect.setFoodbatch(foodbatch);
		detect.setDetector(detector);
		detect.setSubject(subject);
		// 新建记录
		if (detect.getId() == null) {
			if (detect.equals(existingDetect)) {

				mav.addAllObjects(detectService.loadDetectClassReferencedSet(detect));
				// mav = editContinue(mav, detect);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detect/editDetect.jsp");
			} else {
				detectService.saveDetect(detect);
				mav.setViewName("forward:/listDetects");
			}
		}
		// 编辑记录
		else {
			Detect originDetect = CmiHibernateProxyUtil.deproxy(detectDAO.findDetectByPrimaryKey(detect.getId()));
			if (!detect.equals(originDetect) && detect.equals(existingDetect)) {
				mav.addAllObjects(detectService.loadDetectClassReferencedSet(detect));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detect/editDetect.jsp");
			} else {
				detectService.saveDetect(detect);
				mav.setViewName("forward:/listDetects");
			}
		}
		return mav;
	}

	@RequestMapping("/detectController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
